/**
 * AnnotationMemo.java Created on : 5 juil. 07
 */
package fr.limsi.seven;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;

import javax.swing.JDialog;
import javax.swing.JTextPane;

/**
 * @author Camille
 */
public class AnnotationMemo extends Annotation {

	/**
	 * Auto-generated serial version UID number
	 */
	private static final long serialVersionUID = 7908945003358327015L;

	/**
	 * Content displaying shift (in pixels) compared to the mouse position
	 */
	public static final int CONTENT_SHIFT = 5;

	/**
	 * {@link JDialog} displaying the annotation content
	 */
	protected JDialog contentDialog;

	/**
	 * The annotation content
	 */
	protected JTextPane content;

	/**
	 * Default displaying label
	 */
	public static final String LABEL = "Memo";

	/**
	 * Default constructor
	 * 
	 * @param pyramide2D
	 *            the associated {@link Pyramide2D}
	 */
	public AnnotationMemo(Pyramide2D pyramide2D) {

		super(pyramide2D);

		setLayout(new FlowLayout(FlowLayout.CENTER));

		mainColor = Color.WHITE;

		content = new JTextPane();
		content.setEditable(false);
		content.setContentType("text/html");
		content.setBackground(mainColor);
		content.setText("<html>Pas de contenu</html>");

		contentDialog = new JDialog();
		contentDialog.setUndecorated(true);
		contentDialog.setLayout(new FlowLayout(FlowLayout.CENTER));
		contentDialog.setBackground(mainColor);
		contentDialog.add(content);

		buildAdditionnalInteractions();
	}

	/**
	 * Creates and sets specific interactions
	 */
	protected void buildAdditionnalInteractions() {

		MouseAdapter contentAdapter = new MouseAdapter() {
			@Override
			public void mouseEntered(MouseEvent e) {
				contentDialog.pack();
				contentDialog.setLocation(e.getXOnScreen() + CONTENT_SHIFT, e.getYOnScreen() + CONTENT_SHIFT);
				contentDialog.setVisible(true);
			}

			@Override
			public void mouseExited(MouseEvent e) {
				contentDialog.setVisible(false);
			}

			@Override
			public void mouseMoved(MouseEvent e) {
				contentDialog.setLocation(e.getXOnScreen() + CONTENT_SHIFT, e.getYOnScreen() + CONTENT_SHIFT);
			}
		};

		addMouseListener(contentAdapter);
		addMouseMotionListener(contentAdapter);
	}

	@Override
	public void correctControlPointPositions() {
		/* NOTHING TO DO */
	}

	@Override
	public void draw(Graphics2D g2) {

		GeneralPath corner = new GeneralPath();
		corner.moveTo(getWidth() - 2, getHeight() * 0.8f);
		corner.lineTo(getWidth() * 0.8f, getHeight() * 0.8f);
		corner.lineTo(getWidth() * 0.8f, getHeight() - 2);
		corner.closePath();

		g2.setPaint(mainColor);
		g2.fill(new Rectangle2D.Float(0, 0, getWidth() - 2, getHeight() * 0.8f));
		g2.fill(new Rectangle2D.Float(0, 0, getWidth() * 0.8f, getHeight() - 2));

		g2.setPaint(mainColor.darker());
		g2.draw(new Line2D.Float(0, 0, 0, getHeight() - 2));
		g2.draw(new Line2D.Float(0, getHeight() - 2, getWidth() * 0.8f, getHeight() - 2));
		g2.draw(new Line2D.Float(getWidth() - 2, 0, getWidth() - 2, getHeight() * 0.8f));
		g2.draw(corner);
		g2.fill(corner);

		g2.setPaint(Color.BLUE);
		g2.fill(new Rectangle2D.Float(0, 0, getWidth() - 2, getHeight() * 0.2f));

		Rectangle2D labelBBox = getFontMetrics(getFont()).getStringBounds(LABEL, getGraphics());

		if ((labelBBox.getWidth() < getWidth()) && (labelBBox.getHeight() < (getHeight() * 0.8f))) {
			g2.drawString(LABEL, (int) ((getWidth() / 2) - (labelBBox.getWidth() / 2)), (int) ((getHeight() / 2) - (labelBBox.getHeight() / 2)));
		}
	}

	@Override
	public int getAnnotationType() {
		return TYPE_MEMO;
	}

	@Override
	public String getContent() {
		return content.getText();
	}

	@Override
	public void setContent(String content) {
		this.content.setText("<html>" + content + "</html>");
	}

	@Override
	public int getDefaultWidth() {
		return 60;
	}

	@Override
	public int getDefaultHeight() {
		return 70;
	}
}
